package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Examine;
import com.ruoyi.system.domain.InboundTouristTotalStatistics;
import com.ruoyi.system.domain.Statistics;
import com.ruoyi.system.domain.vo.FileUploadVO;
import com.ruoyi.system.mapper.ExamineMapper;
import com.ruoyi.system.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InboundTouristTotalStatisticsMapper;
import com.ruoyi.system.service.IInboundTouristTotalStatisticsService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 入境游客总量统计Service业务层处理
 *
 * @author zhur
 * @date 2025-03-31
 */
@Service
public class InboundTouristTotalStatisticsServiceImpl implements IInboundTouristTotalStatisticsService {
    @Autowired
    private InboundTouristTotalStatisticsMapper inboundTouristTotalStatisticsMapper;

    @Resource
    private FileUploadService fileUploadService;

    @Resource
    private ExamineMapper examineMapper;

    /**
     * 查询入境游客总量统计
     *
     * @param id 入境游客总量统计主键
     * @return 入境游客总量统计
     */
    @Override
    public InboundTouristTotalStatistics selectInboundTouristTotalStatisticsById(Long id) {

        InboundTouristTotalStatistics inboundTouristTotalStatistics =
                inboundTouristTotalStatisticsMapper.selectInboundTouristTotalStatisticsById(id);
        List<FileUploadVO> list = fileUploadService.
                listFileUploadsByBusinessId(inboundTouristTotalStatistics.getBusinessId());
        inboundTouristTotalStatistics.setFiles(list);
        List<Examine> examines = inboundTouristTotalStatistics.getExamines();
        examines.sort((e1, e2) -> e2.getCreateTime().compareTo(e1.getCreateTime()));
        return inboundTouristTotalStatistics;

    }

    /**
     * 查询入境游客总量统计列表
     *
     * @param inboundTouristTotalStatistics 入境游客总量统计
     * @return 入境游客总量统计
     */
    @Override
    public List<InboundTouristTotalStatistics> selectInboundTouristTotalStatisticsList(InboundTouristTotalStatistics inboundTouristTotalStatistics) {
        System.out.println("查询参数: " + inboundTouristTotalStatistics.getReportingUnit());
        startPage();

        // 创建查询条件
        QueryWrapper<InboundTouristTotalStatistics> inboundTouristTotalStatisticsQueryWrapper = new QueryWrapper<>();

        // 先添加其他非空字段的等值查询条件
       // inboundTouristTotalStatisticsQueryWrapper.setEntity(inboundTouristTotalStatistics);

        // 移除默认的等值查询条件，替换为模糊查询
        if (StringUtils.isNotEmpty(inboundTouristTotalStatistics.getReportingUnit())) {
            inboundTouristTotalStatisticsQueryWrapper.like("reporting_unit", inboundTouristTotalStatistics.getReportingUnit());
        }

        // 添加团队编号模糊查询条件
        if (StringUtils.isNotEmpty(inboundTouristTotalStatistics.getTeamNumber())) {
            inboundTouristTotalStatisticsQueryWrapper.like("team_number", inboundTouristTotalStatistics.getTeamNumber());
        }

        // 添加审核状态查询条件
        if (StringUtils.isNotEmpty(inboundTouristTotalStatistics.getTeamNumber())) {
            inboundTouristTotalStatisticsQueryWrapper.eq("status", inboundTouristTotalStatistics.getStatus());
        }

        // 添加 事件类型查询状态
        if (null != inboundTouristTotalStatistics.getEventType()) {
            inboundTouristTotalStatisticsQueryWrapper.eq("event_type", inboundTouristTotalStatistics.getEventType());
        }

        // 添加排序
        inboundTouristTotalStatisticsQueryWrapper.orderByDesc("create_time");

        // 添加权限过滤
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            inboundTouristTotalStatisticsQueryWrapper.eq("create_user", SecurityUtils.getUserId().toString());
        }

        // 执行查询
        List<InboundTouristTotalStatistics> inboundTouristTotalStatisticsList = inboundTouristTotalStatisticsMapper.selectList(inboundTouristTotalStatisticsQueryWrapper);

        return inboundTouristTotalStatisticsList;
    }

    /**
     * 新增入境游客总量统计
     *
     * @param inboundTouristTotalStatistics 入境游客总量统计
     * @return 结果
     */
    @Override
    public int insertInboundTouristTotalStatistics(InboundTouristTotalStatistics inboundTouristTotalStatistics) {
        inboundTouristTotalStatistics.setCreateTime(new Date());
        inboundTouristTotalStatistics.setCreateUser(SecurityUtils.getLoginUser().
                getUser().getUserId().toString());
        inboundTouristTotalStatistics.setStatus(0);
        int insert = inboundTouristTotalStatisticsMapper.insert(inboundTouristTotalStatistics);

        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(inboundTouristTotalStatistics.getId())).setStatus(0).setType(inboundTouristTotalStatistics.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(inboundTouristTotalStatistics.getRemark());
        examineMapper.insert(examine);
        return insert;
    }

    /**
     * 修改入境游客总量统计
     *
     * @param inboundTouristTotalStatistics 入境游客总量统计
     * @return 结果
     */
    @Override
    public int updateInboundTouristTotalStatistics(InboundTouristTotalStatistics inboundTouristTotalStatistics) {
        // 新增审核信息
        Examine examine = new Examine();
        examine.setBusinessId(String.valueOf(inboundTouristTotalStatistics.getId())).setStatus(inboundTouristTotalStatistics.getStatus()).setType(inboundTouristTotalStatistics.getEventType()).setCreateBy(SecurityUtils.getUserId().toString()).setCreateTime(DateUtils.getTime()).setRemark(inboundTouristTotalStatistics.getRemark()).setRemark(inboundTouristTotalStatistics.getRemark());
        examineMapper.insert(examine);
        return inboundTouristTotalStatisticsMapper.updateById(inboundTouristTotalStatistics);
    }

    /**
     * 批量删除入境游客总量统计
     *
     * @param ids 需要删除的入境游客总量统计主键
     * @return 结果
     */
    @Override
    public int deleteInboundTouristTotalStatisticsByIds(Long[] ids) {
        return inboundTouristTotalStatisticsMapper.deleteInboundTouristTotalStatisticsByIds(ids);
    }

    /**
     * 删除入境游客总量统计信息
     *
     * @param id 入境游客总量统计主键
     * @return 结果
     */
    @Override
    public int deleteInboundTouristTotalStatisticsById(Long id) {
        return inboundTouristTotalStatisticsMapper.deleteInboundTouristTotalStatisticsById(id);
    }
}
